package org.tigr.htc.request;

/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

import java.io.FileNotFoundException;
import java.io.IOException;
import javax.naming.ConfigurationException;

import org.apache.log4j.Logger;
import org.tigr.htc.common.HTCConfig;

/**
 * The <b>ProxyServerFactory</b> class is used to create a new IRequest. 
 * You can create a new request by calling newRequest. The method newRequest also
 * can take a string that contains the path to a java properties file to override
 * the default configuration of the request.  The class also checks the system property
 * "htc.request.config" for the location of a properties files. The order of precedence
 * of property files:
 *   1. properties file passed to newRequest.
 *   2. properties file passed as a system property (-D htc.request.config="path").
 *   3. properties file in defaultConfig.
 * 
 */

public class ProxyServerFactory {
    static Logger log = Logger.getLogger(ProxyServerFactory.class);

    public static final String CONDOR_MOCK_SERVER = "mockserver";
    public static final String SGE_MOCK_SERVER = "sgemockserver";
    //
    // static methods only, no class public constructor
    //
    private ProxyServerFactory() { }

    /**
     * <code>newProxyServer</code> creates a new IProxyServer and configures the Proxy
     * Server using the Properties object passed and the default properties file. The
     * final concrete proxy server will use the properties file to configure itself.
     * 
     * @param p_config
     *            a <code>Properties</code> object containing proxy server
     *            configuration info.
     * @return an <code>IProxyServer</code> value
     * @exception IllegalArgumentException
     *                if an error occurs
     * @exception FileNotFoundException
     *                if an error occurs
     * @exception ConfigurationException
     *                if an error occurs
     * @exception IOException
     *                if an error occurs
     */
    public static IProxyServer newProxyServer()
        throws FileNotFoundException, ConfigurationException, IOException {
        // get uri out of properties file
        String uri = HTCConfig.getProperty("URI");

        if (uri == null) {
            throw new ConfigurationException("No server uri specified in " +
                    "configuration file");
        }

        log.debug("URI: " + uri);

        //
        // needs to be changed to check type and construct the correct type
        if (uri.equals(CONDOR_MOCK_SERVER)) {
            String events = HTCConfig.getProperty("EventsFile");
            if (events == null) {
                throw new ConfigurationException(" Mock Server needs an events file.");
            }
            return (IProxyServer) new MockServer(events);
        } else if (uri.equals(SGE_MOCK_SERVER)) {
            // If the user has specified the SGE mock server then instantiate the
            // mock server
            return (IProxyServer) SgeMockServer.getInstance();
        } else { // create a Real ProxyServer
            try {
                return (IProxyServer) RequestMonitor.getInstance(uri);
            } catch (java.net.MalformedURLException e) {
                throw new ConfigurationException(e.getMessage());
            } catch (java.net.UnknownHostException e) {
                throw new ConfigurationException(e.getMessage());
            }
        }
    }

}
